﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

public partial class ShopExpensList : ExtBaseRpt
{
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            txtbdate.Text = DateTime.Now.AddDays(-3).ToString();
            txtedate.Text = DateTime.Now.ToString();
            BtnQureyUser(null, null);
        }
    }
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        string str = " and cast(convert(varchar(20),a.createdate,23)as datetime) between '" + txtbdate.Text + "' and '" + txtedate.Text + "'";
        string sql = @"select s.ShopName,a.*,case when isnull(checked,0) = 0 then '未审' else '已审' end as checkstate from Bas_ShopExpens a
                        inner join V_Shop s on s.ShopCode = a.ShopCode where 1=1 " + str;
        QueryData(this.Store1, mid, sql);
    }
    protected void BtnDelExpen(object sender, DirectEventArgs e)
    {
        string pidstr = "";
        RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;

        foreach (SelectedRow row in sm.SelectedRows)
        {
            pidstr += row.RecordID + ",";
        }
        string sql = "delete from Bas_ShopExpens where pid in ("+pidstr.TrimEnd(',')+")";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            ShowBox("删除成功！");
            BtnQureyUser(null, null);
        }
        else
        {
            ShowBox("删除失败！");
        }
    }
    protected void BtnExpenCheck(object sender, DirectEventArgs e)
    {
        string bdate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
        string pidstr = "";
        RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;

        foreach (SelectedRow row in sm.SelectedRows)
        {
            pidstr += row.RecordID + ",";
        }
        if (mydb.DM_GetDateSet("select * from Bas_ShopExpens where isnull(checked,0) =1 and pid in(" + pidstr.TrimEnd(',') + ")").Tables[0].Rows.Count > 0)
        {
            ShowBox("请不要重复确认！");
            return;
        }
        if (mydb.DM_GetDateSet(@"select * from V_CustomerAccountDtl where saledate='"+bdate+@"'
                and shopcode in(select ShopCode from Bas_ShopExpens where pid in(" + pidstr.TrimEnd(',') + "))").Tables[0].Rows.Count == 0)
        {
            ShowBox("["+bdate+"]的帐单还没有生成，无法审核！");
            return;
        }
        if (mydb.DM_GetDateSet(@"select * from V_CustomerAccountDtl a
                                inner join Bas_ShopExpens b on b.ShopCode = a.ShopCode
                                where b.pid in("+pidstr.TrimEnd(',')+") and a.sjpayclamt2 - b.PayMoney < 0").Tables[0].Rows.Count > 0)
        {
            ShowBox("垫支超出打款金额，无法审核！");
            return;
        } 
        string sql = "update Bas_ShopExpens set paydate ='" + bdate + "',checked =1 where pid in(" + pidstr.TrimEnd(',') + ")";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
            ShowBox("确认成功！");
        else
            ShowBox("操作错误！");
        BtnQureyUser(null, null);
    }
    
    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid,"xls",e.Xml,this.Response,this.Title);
    }
    [DirectMethod(Namespace = "CompanyX")]
    public void Edit2(string pid, string field, string oldValue, string newValue, object customer)
    {
        //if (mydb.DM_GetDateSet("select * from Bas_ShopExpens where pid=" + pid + " and isnull(checked,0) =1").Tables[0].Rows.Count > 0)
        //{
        //    ShowBox("已审的费用，不能作修改！");
        //    this.GridPanel1.GetStore().RejectChanges();
        //    return;
        //}
        if (oldValue != newValue)
        {
            if (mydb.DM_ExecuteNonQuery("update Bas_ShopExpens set " + field + "='" + newValue
                + "',ModifyDate=getdate(),ModifyUser='" + Session["UserCode"] + "' where pid='" + pid + "'") != -1)
            {
                this.GridPanel1.GetStore().CommitChanges();
            }
            else
            {
                X.Msg.Notify(new NotificationConfig()
                {
                    Title = "消息提示",
                    Html = "修改失败！",
                    Width = 250
                }).Show();
            }
        }
    }
}